import requests
from parsel import Selector
import csv
from 获取二手房房源信息页面列表 import main1
import time

COUNT = 1

class GetHouseDetail:
    def __init__(self, url, csv_writer):
        self.url = url
        self.csv_writer = csv_writer
        self.cookies = {
    "BDUSS_BFESS": "IwYmpGSGtLRVpVYVBjR2UyLWp-aVRUTjF1Und2RnpZZmpNcGdpflExM2pjV2hrSVFBQUFBJCQAAAAAAAAAAAEAAACGkFyEc2hha2VyX1oAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAOPkQGTj5EBkVk",
    "BAIDUID_BFESS": "AF6F4FC903A4F9100E8BDC0DB805A199:FG=1",
    "ZFY": "23B1YK:AYksWfLDGiIGG:BDzJI:AmLQMGv0UTgQQlPuw0o:C",
    "ab_sr": "1.0.1_MDc1MmFlZTk5MjdkOGYwNWNiNTVmMzQ2NDBlYjgzZmQxNWVlYzgzMWIyOTI4Y2RmNDNkN2Y0YTlmNjhmNjFjYzgxZGI1NThhYjE4Mzk5ZTU0ZTU5ZGM3Njk1MWFkMGQ4Y2IwZmEwYzc3NzIzZmVjZWQ4NzBiYjY2MTY3MmE2ZDYyNTFkMTliZDRlMTg0MDMwNTZiOTcwNGFiODI3OGY4N2UxYjY5MWMwODRiY2YzZGY2ZGZhYjFjOTBiY2EyYmI3"
}
        self.headers = {
    "authority": "s.anjuke.com",
    "accept": "*/*",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "cache-control": "no-cache",
    "cookie": "sessid=3AA21FBC-03DD-0600-039E-2D42C2F29F17; aQQ_ajkguid=9CECC00C-33E9-DCBF-B2ED-DF9EA2EBB6A7; ctid=27; twe=2; id58=CrIclWWhP59L1z6sLQYNAg==; obtain_by=2; ajk_member_verify=W5dL2pBhDCiir8akZBDo66rAU%2Bv0ns7yeYLQQhwQQ0U%3D; ajk_member_verify2=MjgwNjQwODk2fFVKYm5TcmJ8MQ%3D%3D; ajk-appVersion=; fzq_h=344bdb6b2653855c66f15c465e632a6c_1705066453652_34ca799228f4422d8d38124a06f4a1e8_3740786969; ajk_member_id=280640896; xxzl_cid=d71dd6dc33c84bd19340f93a64c3470b; xxzl_deviceid=bnNnzFn1rNJNAyVjS3IS6Bc44muBh++I8YMgxvFR33vRRkETlizHoKTKLFAzcNgB; fzq_js_anjuke_xiaoqu_pc=278dc41fa6dcf8a1556554d9cc01ddb1_1705067327047_24; ajkAuthTicket=TT=fe650ee3783fa06fe5ba6e6243fb53d7&TS=1705067327950&PBODY=VAIkftyAtFQJCArASFOuCfTJ6H_XEu3a_afHfaGdPOQ6fqMGri8ZYjQVeOerHCDv4l1CSBJvCUje-RnMOq5SERSnO0ddjSeCPEVqYWvlKLphxxI7MDl2zKJZs1Yn6qips2Hn2ufrw3zTY5VggD4gBoeBbsjhw417J1QNMwOcQp4&VER=2&CUID=z0xo8z93-p5iDJq5J3SnqaS2r3T3qS61",
    "pragma": "no-cache",
    "sec-ch-ua": "\"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Microsoft Edge\";v=\"120\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-site",
    "sec-fetch-user": "?1",
    "upgrade-insecure-requests": "1",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0",
    "referer": "https://cs.anjuke.com/",
    "origin": "https://cs.anjuke.com",
    "Accept": "image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Pragma": "no-cache",
    "Referer": "https://cs.anjuke.com/",
    "Sec-Fetch-Dest": "image",
    "Sec-Fetch-Mode": "no-cors",
    "Sec-Fetch-Site": "cross-site",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0",
    "content-type": "multipart/form-data; boundary=----WebKitFormBoundaryMmKUBSPp3a0hgLyg",
    "Content-Type": "text/plain;charset=UTF-8",
    "Origin": "https://cs.anjuke.com"
}
        self.data = {}

    def get_html(self):
        try:
            time.sleep(1)
            response = requests.get(self.url, headers=self.headers, cookies=self.cookies)
            return response.text
        except Exception as e:
            print(e)



    def parse_html(self, html):
        selector = Selector(text=html)
        try:
            # 房源名称
            house_name = selector.css('h1::text').get()
            # 房源售价
            house_price_compare = selector.css('div.house-price_compare span::text').getall()
            house_price_title = selector.css('p.house-price_title::text').get()
            house_price = " ".join(house_price_compare + [house_price_title])
            # 房源信息
            house_info = selector.xpath('//div[@class="info-list"]/div')

            for house in house_info:
                label = house.css('div.label::text').get().strip()
                hover = house.css('div.hover div.hover-inner div.hover-value::text').get()
                if hover is None:
                    hover = house.css('div.value::text').get().strip()
                else:
                    hover = hover.strip()

                self.save_data(label, hover, house_name, house_price)
        except Exception as e:
            pass

    def save_data(self, label, hover, house_name, house_price):
        self.data[label] = hover
        self.data['楼盘名称'] = house_name
        self.data['楼盘价格'] = house_price

    def run(self):
        html = self.get_html()
        self.parse_html(html)
        print(self.data)
        self.csv_writer.writerow(self.data)


def main():
    global COUNT
    # row_url = 'https://cs.anjuke.com/community/view/786141'
    fieldnames = [
        '楼盘名称', '楼盘价格', '物业类型', '权属类别', '竣工时间', '产权年限', '总户数',
        '总建面积', '容积率', '绿化率', '建筑类型', '所属商圈',
        '统一供暖', '供水供电', '停车位', '物业费', '停车费',
        '车位管理费', '物业公司', '小区地址', '开发商',
    ]
    with open('secondhand_house.csv', 'a+', encoding='utf-8-sig', newline='') as f:
        csv_writer = csv.DictWriter(f, fieldnames=fieldnames)
        csv_writer.writeheader()


    for url in main1():
        print('当前是第几个房 = ', COUNT, end='\n\n')
        COUNT = COUNT + 1
        time.sleep(0.5)
        f = open('secondhand_house.csv', 'a+', encoding='utf-8-sig', newline='')
        csv_writer = csv.DictWriter(f, fieldnames=fieldnames)
        spider = GetHouseDetail(url, csv_writer)
        spider.run()
        f.close()


if __name__ == '__main__':
    main()
